package com.jltx.xq.coroutinesandrxpro.coroutines

import android.util.Log
import com.jltx.xq.coroutinesandrxpro.rxjava.RxJava2Learn1
import com.jltx.xq.coroutinesandrxpro.rxjava.RxJava2Learn1.TAG
import com.jltx.xq.coroutinesandrxpro.rxjava.RxJava2Learn1.heavyInitialization
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

/**
 * @author aidenquan
 *         Created by junlintianxia on 2021/07/26.
 */
object CoroutinesLearn1 {
    fun initializeObjects() {
        GlobalScope.launch(Dispatchers.Main) {
            try {
                withContext(Dispatchers.IO) {
                    heavyInitialization()
                }
                Log.d(TAG, "Cor heavyInitialization success ${Thread.currentThread()}")
            } catch (e: Exception) {
                Log.d(TAG, "Cor heavyInitialization fail $e ${Thread.currentThread()}")
            }
        }
    }

    fun fibonacciAsync() {
        GlobalScope.launch(Dispatchers.Main) {
            try {
                val value = withContext(Dispatchers.IO) {
                    RxJava2Learn1.fib(10)
                }
                Log.d(TAG, "Cor fibonacciAsync success $value ${Thread.currentThread()}")
            } catch (e: Exception) {
                Log.d(TAG, "Cor fibonacciAsync fail $e ${Thread.currentThread()}")
            }
        }
    }
}